﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebChatServer.Models
{
    public class ChatRepository
    {
        ChatOnlineEntities entities = new ChatOnlineEntities();

        public void Chat(string sender, string receiver, string message)
        {
            Message mess = new Message();
            mess.UserId = sender;
            mess.FriendId = receiver;
            mess.Message1 = message;

            entities.Messages.AddObject(mess);
            entities.SaveChanges();
        }

        public Message[] Check(string receiver)
        {
            var lstMess = from mess in entities.Messages
                          where mess.FriendId == receiver
                          select mess;

            if (lstMess != null && lstMess.Count() > 0)
            {
                return lstMess.ToArray();
            }

            return null;
        }

        public void Delete(Message message)
        {
            var delmsg = from mess in entities.Messages
                         where mess.Id == message.Id
                         select mess;

            if (delmsg != null)
            {
                entities.Messages.DeleteObject(delmsg.First());
                entities.SaveChanges();
            }
        }

        public Status[] CheckStatus()
        {
            var lstSta = from sta in entities.Status
                          select sta;

            if (lstSta != null && lstSta.Count() > 0)
            {
                return lstSta.ToArray();
            }

            return null;
        }

        public void Status(string id, int status)
        {
            var lststa = from stas in entities.Status
                          where stas.Id == id
                          select stas;

            if (!(lststa != null && lststa.Count() > 0))
            {
                Status sta = new Status();
                sta.Id = id;
                sta.Status1 = status;
                entities.Status.AddObject(sta);
                entities.SaveChanges();
            }         
        }

        public void DeleteStatus(string senderId)
        {
            var delsta = from sta in entities.Status
                         where sta.Id == senderId
                         select sta;

            if (delsta != null)
            {
                entities.Status.DeleteObject(delsta.First());
                entities.SaveChanges();
            }
        }

        public void FileSend(string senderid, string fileName, string receiverid)
        {
            FileSend fileSend = new FileSend();
            fileSend.UserId = senderid;
            fileSend.FriendId = receiverid;
            fileSend.FileName = fileName;

            entities.FileSends.AddObject(fileSend);
            entities.SaveChanges();
        }

        public FileSend[] CheckFileSend(string receiverid)
        {
            var lstFile = from file in entities.FileSends
                          where file.FriendId == receiverid
                         select file;

            if (lstFile != null && lstFile.Count() > 0)
            {
                return lstFile.ToArray();
            }

            return null;
        }

        public void DeleteFile(FileSend fileSend)
        {
            var delfile = from file in entities.FileSends
                         where file.UserId == fileSend.UserId 
                            && file.FriendId == fileSend.FriendId 
                            && file.FileName == fileSend.FileName
                         select file;

            if (delfile != null)
            {
                entities.FileSends.DeleteObject(delfile.First());
                entities.SaveChanges();
            }
        }
    }
}